package xwj.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import xwj.example.demo.dao.SupplierDao;
import xwj.example.demo.domain.Product;
import xwj.example.demo.dao.ProductDao;
import xwj.example.demo.domain.Supplier;
import xwj.example.demo.exception.ProductAdminException;
import xwj.example.demo.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwj
 * @since 2022-10-30
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductDao, Product> implements IProductService {

    public Product addProduct(Product product) throws ProductAdminException {
        if (product.getId() != null) {
            if (this.getById(product.getId()) != null) {
                throw new ProductAdminException("添加商品失败：商品已经存在");
            }
        }
        this.baseMapper.insert(product);
        for(Supplier supplier : product.getSupplierList()) {
            this.baseMapper.insertProductSupplier(product.getId(), supplier.getId());
        }
        return product;
    }

    /**
     * 根据Id查找
     *
     * @param id
     * @return
     */
    public Product getProduct(long id) {
        return this.getById(id);
    }
    /**
     * 更新商品信息
     *
     * @param id
     * @param product
     */
    @Transactional
    public void updateProduct(long id, Product product) throws  ProductAdminException{
        if (this.getById(id) == null) {
            throw new ProductAdminException("修改商品失败：商品不存在");
        }
        try{
        this.baseMapper.updateById(product);
        this.baseMapper.deleteSupplierByProduct(product.getId());
        for (Supplier supplier : product.getSupplierList()) {
            this.baseMapper.insertProductSupplier(product.getId(), supplier.getId());
        }
        }
        catch(Exception e){
            throw new ProductAdminException(e.getMessage());
        }
    }

    /**
     * 删除商品
     *
     * @param id
     */
    public void deleteProduct(long id) {
        this.removeById(id);
        this.baseMapper.deleteSupplierByProduct(id);
    }

    public IPage findProduct( Map<String,Object> condition,int pageNum,int pageSize){
        Page<Product> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.like(condition.containsKey("name"), "p.name", condition.get("name"));
        qw.le(condition.containsKey("price"), "p.price", condition.get("price"));
        qw.eq(condition.containsKey("productType"), "p.product_type", condition.get("productType"));
        qw.like(condition.containsKey("description"), "p.description", condition.get("description"));
        qw.ge(condition.containsKey("stockQuantity"), "p.stock_quantity", condition.get("stockQuantity"));
        qw.eq(condition.containsKey("category"), "p.category", condition.get("category"));
        if(condition.containsKey("supplierName")){
            qw.eq("s.name", condition.get("supplierName"));
            this.baseMapper.findProductsBySupplier(page, qw); //多表查询
        }else{
            this.baseMapper.findProducts(page, qw); //单表查询
        }
        return page;
    }
}
